package com.appkubes.portal.admin.application.app.push.util;

import com.appkubes.portal.admin.application.app.push.entity.TransmissionContent;
import com.gexin.fastjson.JSON;
import com.gexin.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.notify.Notify;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;

import java.util.ArrayList;
import java.util.List;

public class PushUtil {

	private static String appId = "i0JLEFCJk56hEr8FLsDfT6";
	private static String appKey = "h52thJLTOgAS01br8LvdT5";
	private static String masterSecret = "taWx1hG4Pl6MnmxyPb6Rj6";
	static String host = "http://sdk.open.api.igexin.com/apiex.htm";

	/**
	 * 全局推送
	 * @param transmissionContent
	 * @return
	 */
	public static String allPush(TransmissionContent transmissionContent){
		IGtPush push = new IGtPush(host, appKey, masterSecret);
		NotificationTemplate template = getNotificationTemplate(transmissionContent.getTitle(),transmissionContent.getContent(),transmissionContent);
		List<String> appIds = new ArrayList<>();
		appIds.add(appId);
		AppMessage message = new AppMessage();
		message.setData(template);
		message.setAppIdList(appIds);
		message.setOffline(true);
		message.setOfflineExpireTime(1000 * 600);  // 时间单位为毫秒

		// STEP6：执行推送
		IPushResult ret = push.pushMessageToApp(message);
		if (ret != null) {
			System.out.println(ret.getResponse().toString());
			return new JSONObject(ret.getResponse()).getString("result");
		} else {
			return "消息推送出错";
		}
	}

	/**
	 * 单推
	 * @param cid
	 * @param transmissionContent
	 * @return
	 */
	public static String singlePush(String cid, TransmissionContent transmissionContent){
		IGtPush push = new IGtPush(host, appKey, masterSecret);
		NotificationTemplate template = getNotificationTemplate(transmissionContent.getTitle(),transmissionContent.getContent(),transmissionContent);
		SingleMessage message = new SingleMessage();
		message.setOffline(true);
		// 离线有效时间，单位为毫秒
		message.setOfflineExpireTime(24 * 3600 * 1000);
		message.setData(template);
		// 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
		message.setPushNetWorkType(0);
		Target target = new Target();
		target.setAppId(appId);
		target.setClientId(cid);
		//target.setAlias(Alias);
		IPushResult ret = null;
		try {
			ret = push.pushMessageToSingle(message, target);
		} catch (RequestException e) {
			e.printStackTrace();
			ret = push.pushMessageToSingle(message, target, e.getRequestId());
		}
		if (ret != null) {
			System.out.println(ret.getResponse().toString());
			return new JSONObject(ret.getResponse()).getString("result");
		} else {
			return "消息推送出错";
		}
	}

	/**
	 * 消息模板
	 * @param title
	 * @param content
	 * @param transmissionContent
	 * @return
	 */
	public static NotificationTemplate getNotificationTemplate(String title,String content, TransmissionContent transmissionContent) {
		NotificationTemplate template = new NotificationTemplate();
		// 设置APPID与APPKEY
		template.setAppId(appId);
		template.setAppkey(appKey);

		Style0 style = new Style0();
		// 设置通知栏标题与内容
		style.setTitle(title);
		style.setText(content);
		// 配置通知栏图标
		style.setLogo("icon.png");
		// 配置通知栏网络图标
		style.setLogoUrl("");
		// 设置通知是否响铃，震动，或者可清除
//		style.setRing(true);
//		style.setVibrate(true);
//		style.setClearable(true);
//		style.setChannel("通知渠道id");
//		style.setChannelName("通知渠道名称");
//		style.setChannelLevel(3); //设置通知渠道重要性
		template.setStyle(style);

		template.setTransmissionType(2);  // 透传消息接受方式设置，1：立即启动APP，2：客户端收到消息后需要自行处理
		template.setTransmissionContent(JSON.toJSONString(transmissionContent));
		template.setAPNInfo(getAPNPayload(title,content,transmissionContent)); //ios消息推送
//		template.set3rdNotifyInfo(get3rdNotifyInfo(transmissionContent));
//		template.setAPNInfo(getVoIPPayload());
		return template;
	}

	private static APNPayload getAPNPayload(String title,String content, TransmissionContent transmissionContent) {
		APNPayload payload = new APNPayload();
		//在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
//		payload.setAutoBadge("+1");
//		payload.setContentAvailable(1);
		//ios 12.0 以上可以使用 Dictionary 类型的 sound
//		payload.setSound("default");
//		payload.setCategory("$由客户端定义");
		payload.addCustomMsg("type", transmissionContent.getType());
		payload.addCustomMsg("param", transmissionContent.getParam());

		//简单模式APNPayload.SimpleMsg
		payload.setAlertMsg(new APNPayload.DictionaryAlertMsg().setTitle(title).setSubtitle(content));
		//payload.setAlertMsg(getDictionaryAlertMsg());  //字典模式使用APNPayload.DictionaryAlertMsg

		//设置语音播报类型，int类型，0.不可用 1.播放body 2.播放自定义文本
//		payload.setVoicePlayType(2);
		//设置语音播报内容，String类型，非必须参数，用户自定义播放内容，仅在voicePlayMessage=2时生效
		//注：当"定义类型"=2, "定义内容"为空时则忽略不播放
//		payload.setVoicePlayMessage("定义内容");

		// 添加多媒体资源
//		payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.pic)
//				.setResUrl("资源文件地址")
//				.setOnlyWifi(true));

		return payload;
	}
	/**
	 * 第三方厂商通知
	 *
	 * @param transmissionContent
	 * @return
	 */
	private static Notify get3rdNotifyInfo(TransmissionContent transmissionContent) {
		Notify notify = new Notify();
		notify.setTitle(transmissionContent.getTitle());
		notify.setContent(transmissionContent.getContent());
		notify.setPayload(JSON.toJSONString(transmissionContent));
		return notify;
	}
}
